{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a163be0b-beec-4485-bfbf-bcad35639b42",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8f78281-c0d3-48c4-8623-547479c3c529",
   "metadata": {},
   "source": [
    "# 数据读取-图像\n",
    "- cv2.IMREAD_GRAYSCALE 读取灰度图\n",
    "- cv2.IMREAD_COLOR 读取灰度图\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "81d55f5b-6530-4585-a99f-0c3e6702c38d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 图像读取\n",
    "image = cv2.imread('../../../../opencv_project/demo02/src/demo02/1.jpg',cv2.IMREAD_COLOR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9f26851c-be88-448e-941a-15ca135e69b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "def cv_show(name,image):\n",
    "    # 图像的读取，也可以创建多个窗口\n",
    "    cv2.imshow(name,image)\n",
    "    # 等待时间，毫秒级，0表示按任意键终止\n",
    "    cv2.waitKey(0)\n",
    "    # 关闭所有窗口\n",
    "    cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "ece5bbf9-71a1-4eae-9e69-b44efcaf1d6c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# demo\n",
    "cv_show('test', image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "bf62e4dc-5082-4c3b-b295-abaac3667008",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(425, 977, 3)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 图像属性，BGR\n",
    "image.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "ece37330-3f85-4691-80a7-bce1429c9044",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取灰度图\n",
    "image = cv2.imread('../../../../opencv_project/demo02/src/demo02/1.jpg',cv2.IMREAD_GRAYSCALE)\n",
    "cv_show('test1', image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "fb90390c-213b-4a3e-abaf-6b4b28563d7c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(425, 977)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "image.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "1d5376c2-4e92-4992-a654-f322e6610d52",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1245675"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "image.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "ee248e71-7a86-4676-aba2-4d94cf2a84bf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "a3c5cdbc-9875-4d29-9e4b-4a2452243f4d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('uint8')"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "image.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "e09fdf80-cb72-4adb-b2a5-0ab040ef04ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 保存\n",
    "cv2.imwrite('C:/Users/lhl/Desktop/1.jpg',image)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
